package com.heying.springcore.aop.jdbc.repository.jdbc;

import com.heying.springcore.aop.jdbc.entity.ClassInfo;
import com.heying.springcore.aop.jdbc.repository.ClassRepository;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.inject.Inject;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class JdbcClassRepository implements ClassRepository {

    private JdbcTemplate jdbcTemplate;

    private ClassRepository classRepository;



    @Inject
    public JdbcClassRepository(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public void insertOne(ClassInfo classInfo) {
        jdbcTemplate.update("insert into test01.tbl_classinfo values (?,?)",
                classInfo.getClassNo(),
                classInfo.getClassName());

    }

    public List<ClassInfo> queryClassByStuno(String stuno){
        return jdbcTemplate.query("select * from test01.tbl_classinfo c join test01.tbl_scoreinfo sc on " +
                "c.classno = sc.classno where sc.stuno = ?",this::mapToClass,stuno);
    }

    public ClassInfo mapToClass(ResultSet rs,int row) throws SQLException {
        return new ClassInfo(rs.getString("classno"),
                rs.getString("classname"));
    }
}
